/*
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "stdio.h"
#include <climits>
#include <gtest/gtest.h>

#include "It_posix_container.h"
#define TEST_ARG 0

std::string gen_container_link_path(int pid, const std::string& containerType) {
    std::ostringstream buf;
    buf << "/proc/" << pid << "/container/" << containerType;
    return buf.str();
}

std::string readlink_container(int pid, const std::string& containerType) {
    char buf[PATH_MAX];
    auto path = gen_container_link_path(pid, containerType);

    struct stat sb;
    int ret = lstat(path.data(), &sb);
    if (ret == -1) {
        throw std::exception();
    }

    size_t bufsiz = sb.st_size + 1;
    if (sb.st_size == 0)
        bufsiz = PATH_MAX;

    ssize_t nbytes = readlink(path.c_str(), buf, bufsiz);
    if (nbytes == -1) {
        throw std::exception();
    }
    return buf;
}

int ChildFunction(void *args)
{
    sleep(2);
    return 0;
}

pid_t CloneWrapper(int (*func)(void *), int flag, void *args)
{
    char *stack;
    char *stackTop;
    stack = (char*)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
    stackTop = stack + STACK_SIZE;

    return clone(func, stackTop, flag , args);
}

using namespace testing::ext;
namespace OHOS {
class PosixContainerTest : public testing::Test {
public:
    static void SetUpTestCase(void) {}
    static void TearDownTestCase(void) {}
};

#if defined(LOSCFG_USER_TEST_SMOKE)
HWTEST_F(PosixContainerTest, ItPosixContainer002, TestSize.Level0)
{
    ItPosixContainer002();
}
HWTEST_F(PosixContainerTest, ItPosixContainer003, TestSize.Level0)
{
    ItPosixContainer003();
}

/* *
 * @tc.name: IT_POSIX_CONTAINER_004
 * @tc.desc: function for PosixContainerTest
 * @tc.type: FUNC
 */
#endif

#if defined(LOSCFG_USER_TEST_FULL)
HWTEST_F(PosixContainerTest, ItPosixContainer001, TestSize.Level0)
{
    ItPosixContainer001();
}
HWTEST_F(PosixContainerTest, ItPosixContainer001NET, TestSize.Level0)
{
    ItPosixContainer001NET();
}
HWTEST_F(PosixContainerTest, ItPosixContainer001NS, TestSize.Level0)
{
    ItPosixContainer001NS();
}
HWTEST_F(PosixContainerTest, ItPosixContainer001FLAGS, TestSize.Level0)
{
    ItPosixContainer001FLAGS();
}
#if defined(LOSCFG_USER_TEST_PID_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainer001PID, TestSize.Level0)
{
    ItPosixContainer001PID();
}
#endif
#if defined(LOSCFG_USER_TEST_USER_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainer001USER, TestSize.Level0)
{
    ItPosixContainer001USER();
}
#endif
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainer001UTS, TestSize.Level0)
{
    ItPosixContainer001UTS();
}
#endif
HWTEST_F(PosixContainerTest, ItPosixContainer004, TestSize.Level0)
{
    ItPosixContainer004();
}
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainer006, TestSize.Level0)
{
    ItPosixContainer006();
}
HWTEST_F(PosixContainerTest, ItPosixContainer007, TestSize.Level0)
{
    ItPosixContainer007();
}
HWTEST_F(PosixContainerTest, ItPosixContainer008, TestSize.Level0)
{
    ItPosixContainer008();
}
#endif
HWTEST_F(PosixContainerTest, ItPosixContainer009, TestSize.Level0)
{
    ItPosixContainer009();
}
HWTEST_F(PosixContainerTest, ItPosixContainer010, TestSize.Level0)
{
    ItPosixContainer010();
}

#if defined(LOSCFG_USER_TEST_USER_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainer011, TestSize.Level0)
{
    ItPosixContainer011();
}
HWTEST_F(PosixContainerTest, ItPosixContainer012, TestSize.Level0)
{
    ItPosixContainer012();
}
HWTEST_F(PosixContainerTest, ItPosixContainer013, TestSize.Level0)
{
    ItPosixContainer013();
}
#endif

HWTEST_F(PosixContainerTest, ItPosixContainer014, TestSize.Level0)
{
    ItPosixContainer014();
}
#if defined(LOSCFG_USER_TEST_MNT_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainerMnt001, TestSize.Level0)
{
    ItPosixContainerMnt001();
}
HWTEST_F(PosixContainerTest, ItPosixContainerMnt002, TestSize.Level0)
{
    ItPosixContainerMnt002();
}
HWTEST_F(PosixContainerTest, ItPosixContainerMnt003, TestSize.Level0)
{
    ItPosixContainerMnt003();
}
HWTEST_F(PosixContainerTest, ItPosixContainerMnt004, TestSize.Level0)
{
    ItPosixContainerMnt004();
}
HWTEST_F(PosixContainerTest, ItPosixContainerMnt005, TestSize.Level0)
{
    ItPosixContainerMnt005();
}
HWTEST_F(PosixContainerTest, ItPosixContainerMnt006, TestSize.Level0)
{
    ItPosixContainerMnt006();
}
HWTEST_F(PosixContainerTest, ItPosixContainerMnt007, TestSize.Level0)
{
    ItPosixContainerMnt007();
}
HWTEST_F(PosixContainerTest, ItPosixContainerMnt008, TestSize.Level0)
{
    ItPosixContainerMnt008();
}
#endif
#if defined(LOSCFG_USER_TEST_NET_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainerNet001, TestSize.Level0)
{
    ItPosixContainerNet001();
}
HWTEST_F(PosixContainerTest, ItPosixContainerNet002, TestSize.Level0)
{
    ItPosixContainerNet002();
}
HWTEST_F(PosixContainerTest, ItPosixContainerNet003, TestSize.Level0)
{
    ItPosixContainerNet003();
}
HWTEST_F(PosixContainerTest, ItPosixContainerNet004, TestSize.Level0)
{
    ItPosixContainerNet004();
}
HWTEST_F(PosixContainerTest, ItPosixContainerNet005, TestSize.Level0)
{
    ItPosixContainerNet005();
}
HWTEST_F(PosixContainerTest, ItPosixContainerNet006, TestSize.Level0)
{
    ItPosixContainerNet006();
}
HWTEST_F(PosixContainerTest, ItPosixContainerNet007, TestSize.Level0)
{
    ItPosixContainerNet007();
}
HWTEST_F(PosixContainerTest, ItPosixContainerNet008, TestSize.Level0)
{
    ItPosixContainerNet008();
}
HWTEST_F(PosixContainerTest, ItPosixContainerNet009, TestSize.Level0)
{
    ItPosixContainerNet009();
}
#endif
#if defined(LOSCFG_USER_TEST_TIME_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainerTime001, TestSize.Level0)
{
    ItPosixContainerTime001();
}
HWTEST_F(PosixContainerTest, ItPosixContainerTime002, TestSize.Level0)
{
    ItPosixContainerTime002();
}
HWTEST_F(PosixContainerTest, ItPosixContainerTime003, TestSize.Level0)
{
    ItPosixContainerTime003();
}
HWTEST_F(PosixContainerTest, ItPosixContainerTime004, TestSize.Level0)
{
    ItPosixContainerTime004();
}
HWTEST_F(PosixContainerTest, ItPosixContainerTime005, TestSize.Level0)
{
    ItPosixContainerTime005();
}
HWTEST_F(PosixContainerTest, ItPosixContainerTime006, TestSize.Level0)
{
    ItPosixContainerTime006();
}
HWTEST_F(PosixContainerTest, ItPosixContainerTime007, TestSize.Level0)
{
    ItPosixContainerTime007();
}
HWTEST_F(PosixContainerTest, ItPosixContainerTime008, TestSize.Level0)
{
    ItPosixContainerTime008();
}
HWTEST_F(PosixContainerTest, ItPosixContainerTime009, TestSize.Level0)
{
    ItPosixContainerTime009();
}
#endif
#if defined(LOSCFG_USER_TEST_IPC_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainerIpc001, TestSize.Level0)
{
    ItPosixContainerIpc001();
}
HWTEST_F(PosixContainerTest, ItPosixContainerIpcShmClone001, TestSize.Level0)
{
    ItPosixContainerIpcShmClone001();
}
HWTEST_F(PosixContainerTest, ItPosixContainerIpcShmUnshare001, TestSize.Level0)
{
    ItPosixContainerIpcShmUnshare001();
}
HWTEST_F(PosixContainerTest, ItPosixContainerIpcShmSetns001, TestSize.Level0)
{
    ItPosixContainerIpcShmSetns001();
}
HWTEST_F(PosixContainerTest, ItPosixContainerIpcMq001, TestSize.Level0)
{
    ItPosixContainerIpcMq001();
}
HWTEST_F(PosixContainerTest, ItPosixContainerIpcMq002, TestSize.Level0)
{
    ItPosixContainerIpcMq002();
}
HWTEST_F(PosixContainerTest, ItPosixContainerIpcMq003, TestSize.Level0)
{
    ItPosixContainerIpcMq003();
}

#endif
#endif

#if defined(LOSCFG_USER_TEST_PID_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixPidContainer001, TestSize.Level0)
{
    ItPosixPidContainer001();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer002, TestSize.Level0)
{
    ItPosixPidContainer002();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer003, TestSize.Level0)
{
    ItPosixPidContainer003();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer004, TestSize.Level0)
{
    ItPosixPidContainer004();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer005, TestSize.Level0)
{
    ItPosixPidContainer005();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer006, TestSize.Level0)
{
    ItPosixPidContainer006();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer007, TestSize.Level0)
{
    ItPosixPidContainer007();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer008, TestSize.Level0)
{
    ItPosixPidContainer008();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer009, TestSize.Level0)
{
    ItPosixPidContainer009();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer010, TestSize.Level0)
{
    ItPosixPidContainer010();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer011, TestSize.Level0)
{
    ItPosixPidContainer011();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer012, TestSize.Level0)
{
    ItPosixPidContainer012();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer013, TestSize.Level0)
{
    ItPosixPidContainer013();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer014, TestSize.Level0)
{
    ItPosixPidContainer014();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer015, TestSize.Level0)
{
    ItPosixPidContainer015();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer016, TestSize.Level0)
{
    ItPosixPidContainer016();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer017, TestSize.Level0)
{
    ItPosixPidContainer017();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer018, TestSize.Level0)
{
    ItPosixPidContainer018();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer019, TestSize.Level0)
{
    ItPosixPidContainer019();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer020, TestSize.Level0)
{
    ItPosixPidContainer020();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer021, TestSize.Level0)
{
    ItPosixPidContainer021();
}
HWTEST_F(PosixContainerTest, ItPosixPidContainer022, TestSize.Level0)
{
    ItPosixPidContainer022();
}
#endif

#if defined(LOSCFG_USER_TEST_STABILITY)
HWTEST_F(PosixContainerTest, ItPosixContainerStability001, TestSize.Level0)
{
    ItPosixContainerStability001();
}
#if defined(LOSCFG_USER_TEST_USER_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainerStability002, TestSize.Level0)
{
    ItPosixContainerStability002();
}
#endif
HWTEST_F(PosixContainerTest, ItPosixContainerStability003, TestSize.Level0)
{
    ItPosixContainerStability003();
}
#if defined(LOSCFG_USER_TEST_NET_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainerStability004, TestSize.Level0)
{
    ItPosixContainerStability004();
}
#endif
#if defined(LOSCFG_USER_TEST_PID_CONTAINER)
HWTEST_F(PosixContainerTest, ItPosixContainerStability005, TestSize.Level0)
{
    ItPosixContainerStability005();
}
#endif
#endif

} // namespace OHOS
